package yja.gzw.app.activity

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.provider.MediaStore
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.Observer
import cn.jzvd.Jzvd
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.kwai.auth.common.KwaiConstants
import com.kwai.opensdk.sdk.model.base.OpenSdkConfig
import com.kwai.opensdk.sdk.model.postshare.PostShareMediaInfo
import com.kwai.opensdk.sdk.model.postshare.SingleVideoPublish
import com.kwai.opensdk.sdk.model.postshare.plc.MiniProgramPlcBindInfo
import com.kwai.opensdk.sdk.openapi.IKwaiOpenAPI
import com.kwai.opensdk.sdk.openapi.KwaiOpenAPI
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import yja.gzw.app.R
import yja.gzw.app.activity.FabusanActivity.Companion
import yja.gzw.app.base.BaseActivity
import yja.gzw.app.databinding.ActivityShipingBinding
import yja.gzw.app.model.UserData
import yja.gzw.app.viewmodel.Mainviewmodel
import java.io.File

class ShipingActivity :BaseActivity<Mainviewmodel, ActivityShipingBinding>(R.layout.activity_shiping){

    var file: File?=null
    var paths=""
    override fun initView() {
        val ids =intent.getStringExtra("id")
        val path =intent.getStringExtra("path")



//
//        binding.videoView.setUp(path, "播放视频", Jzvd.SCREEN_WINDOW_NORMAL)
//        binding. videoView.thumbImageView.setImageURI(Uri.parse("http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640"));
        binding.xiayibu.setOnClickListener {

            if(!UserData.Kuaishoushouquan){
                Toast.makeText(this, "请授权平台", Toast.LENGTH_SHORT).show()
                val itnnet=Intent(this,BasedingzhanghaoActivity::class.java)
                startActivity(itnnet)

            }else{

                val itnnet=Intent(this,WodezhanghuaActivity::class.java)
                UserData.files=file

                itnnet.putExtra("paths",paths)
                itnnet.putExtra("idid",ids)

                startActivity(itnnet)
            }





              //  publishSingleVideo(file!!)


            //todo 发布的基本信息
//            Log.e("基本信息",UserData.token+"    "+ids)
//           viewmodel.onshangchuandingdanshipingwang(UserData.token, mapOf("order_id" to ids.toString(),"video_url" to paths))





        }

        binding.jiajia.setOnClickListener {
            Toast.makeText(this, "视频上传中请稍后。。。", Toast.LENGTH_SHORT).show()
            openGallery()


        }



    }

    override fun dataObservese() {

        viewmodel.onshangchuandingdanshiping.observe(this, Observer {
            if(it.code==1){
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
                val intne=Intent(this, ChengjieActivity::class.java)

                startActivity(intne)
                finish()

            }else{
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
            }


        })


        viewmodel.onlusc.observe(this, Observer {
            if(it.code==1){

                Toast.makeText(this, "上传成功", Toast.LENGTH_SHORT).show()
                val shipinglujin= "http://117ai.32.hzgqapp.com"+it.data.url

                UserData.hechengdeshipinglianjie=shipinglujin
                Log.e("视频上传的路径", shipinglujin)


                paths=shipinglujin
                Glide.with(this)
                    .setDefaultRequestOptions(
                        RequestOptions()
                            .frame(0)
                            .centerCrop()
                    )
                    .load(shipinglujin)
                    .into(binding.tutu)

            }


        })
    }



    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == GALLERY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            data?.data?.let { uri ->
                file = File(getRealPathFromURI(uri))

                val requestFile = file?.asRequestBody("multipart/form-data".toMediaTypeOrNull())
                val body = MultipartBody.Part.createFormData("file", file?.name,requestFile!!)
                viewmodel.onluying(body)
            }
        }
    }
    private fun openGallery() {
        val intent = Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI)
        startActivityForResult(intent, GALLERY_REQUEST_CODE)
    }


    private fun getRealPathFromURI(contentUri: Uri): String {
        val projection = arrayOf(MediaStore.Images.Media.DATA)
        val cursor = contentResolver.query(contentUri, projection, null, null, null)
        val column_index = cursor?.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
        cursor?.moveToFirst()
        val result = cursor?.getString(column_index!!)
        cursor?.close()
        return result!!
    }
    companion object {
        private const val GALLERY_REQUEST_CODE = 1001
    }
}